﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Thinkment.Data;
using We7.CMS.Common;
using We7.CMS.WebControls;
using We7.CMS.WebControls.Core;
using We7.Framework;
using We7.Framework.Util;

namespace We7.CMS.Web.Widgets
{
    [ControlGroupDescription(Label = "最新动态", Icon = "最新动态", Description = "最新动态", DefaultType = "LatestNews.LatestTopPicture")]
    [ControlDescription(Desc = "新闻动态+最新推荐+最新图文", Author = "老大")]
    public partial class LatestNews_LatestTopPictureEx : ThinkmentDataControl
    {
        private List<Article> latestNews, recommends, pictureNews;
        private Article headNews;

        protected ArticleHelper ArticleHelper
        {
            get
            {
                return HelperFactory.GetHelper<ArticleHelper>();
            }
        }

        #region 栏目
        /// <summary>
        /// 栏目ID1
        /// </summary>
        [Parameter(Title = "栏目一", Type = "Channel", Required = true, Weight = 10)]
        public string OwnerID1 = String.Empty;

        /// <summary>
        /// 栏目ID2
        /// </summary>
        [Parameter(Title = "栏目二", Type = "Channel", Required = true, Weight = 9)]
        public string OwnerID2 = String.Empty;

        /// <summary>
        /// 栏目ID3
        /// </summary>
        [Parameter(Title = "栏目三", Type = "Channel", Required = true, Weight = 8)]
        public string OwnerID3 = String.Empty;

        /// <summary>
        /// 栏目ID4
        /// </summary>
        [Parameter(Title = "栏目四", Type = "Channel", Required = false, Weight = 7)]
        public string OwnerID4 = String.Empty;

        /// <summary>
        /// 栏目ID5
        /// </summary>
        [Parameter(Title = "栏目五", Type = "Channel", Required = false, Weight = 6)]
        public string OwnerID5 = String.Empty;

        /// <summary>
        /// 栏目ID6
        /// </summary>
        [Parameter(Title = "栏目六", Type = "Channel", Required = false, Weight = 5)]
        public string OwnerID6 = String.Empty;

        /// <summary>
        /// 栏目ID7
        /// </summary>
        [Parameter(Title = "栏目七", Type = "Channel", Required = false, Weight = 4)]
        public string OwnerID7 = String.Empty;

        /// <summary>
        /// 栏目ID8
        /// </summary>
        [Parameter(Title = "栏目八", Type = "Channel", Required = false, Weight = 3)]
        public string OwnerID8 = String.Empty;

        /// <summary>
        /// 栏目ID9
        /// </summary>
        [Parameter(Title = "栏目九", Type = "Channel", Required = false, Weight = 2)]
        public string OwnerID9 = String.Empty;

        /// <summary>
        /// 栏目ID10
        /// </summary>
        [Parameter(Title = "栏目十", Type = "Channel", Required = false, Weight = 1)]
        public string OwnerID10 = String.Empty;
        #endregion

        /// <summary>
        /// 显示的文字新闻记录数
        /// </summary>
        [Parameter(Title = "文字信息记录条数", Type = "Number", DefaultValue = "6")]
        public int PageSize = 6;

        /// <summary>
        /// 轮显广告记录条数
        /// </summary>
        [Parameter(Title = "轮显广告记录条数", Type = "Number", DefaultValue = "5")]
        public int SliderSize = 5;

        /// <summary>
        /// 文字信息日期格式
        /// </summary>
        [Parameter(Title = "文字信息日期格式", Type = "String", DefaultValue = "[MM-dd]")]
        public string DateFormat = "[MM-dd]";


        [Parameter(Title = "标题长度", Type = "Number", DefaultValue = "30")]
        public int TitleLength = 30;

        /// <summary>
        /// 自定义Css类名称
        /// </summary>
        [Parameter(Title = "自定义Css类名称", Type = "String", DefaultValue = "LatestNews_LatestTopPicture")]
        public string CssClass;

        /// <summary>
        /// 缩略图标签
        /// </summary>
        [Parameter(Title = "缩略图标签", Type = "KeyValueSelector", Data = "thumbnail", DefaultValue = "flash")]
        public string ThumbnailTag = "flash";

        /// <summary>
        /// Tags标签
        /// </summary>
        [Parameter(Title = "Tags标签", Type = "Tags", DefaultValue = "")]
        public string Tags;

        /// <summary>
        /// 自定义图标样式
        /// </summary>
        [Parameter(Title = "自定义图标样式", Type = "CustomImage", DefaultValue = "")]
        public string Icon;

        /// <summary>
        /// 是否按置顶排序
        /// </summary>
        [Parameter(Title = "是否显示置顶", Type = "Boolean", DefaultValue = "0", Required = true)]
        public bool IsShow;

        /// <summary>
        /// 
        /// </summary>
        [Parameter(Title = "包含子栏目", Type = "Boolean", DefaultValue = "0", Required = true)]
        public bool HasChildrenChannel;

        /// <summary>
        /// 自定义的css样式
        /// </summary>
        protected virtual string Css
        {
            get
            {
                return CssClass;
            }
        }

        private List<Channel> channellist;
        /// <summary>
        /// 栏目列表:带文章
        /// </summary>
        protected List<Channel> ChannelList
        {
            get
            {
                if (channellist == null)
                {
                    #region 将非空的栏目ID添加到 ListOwnerID
                    List<string> ListOwnerID = new List<string>(); ;
                    if (!string.IsNullOrEmpty(OwnerID1))
                        ListOwnerID.Add(OwnerID1);
                    if (!string.IsNullOrEmpty(OwnerID2))
                        ListOwnerID.Add(OwnerID2);
                    if (!string.IsNullOrEmpty(OwnerID3))
                        ListOwnerID.Add(OwnerID3);
                    if (!string.IsNullOrEmpty(OwnerID4))
                        ListOwnerID.Add(OwnerID4);
                    if (!string.IsNullOrEmpty(OwnerID5))
                        ListOwnerID.Add(OwnerID5);
                    if (!string.IsNullOrEmpty(OwnerID6))
                        ListOwnerID.Add(OwnerID6);
                    if (!string.IsNullOrEmpty(OwnerID7))
                        ListOwnerID.Add(OwnerID7);
                    if (!string.IsNullOrEmpty(OwnerID8))
                        ListOwnerID.Add(OwnerID8);
                    if (!string.IsNullOrEmpty(OwnerID9))
                        ListOwnerID.Add(OwnerID9);
                    if (!string.IsNullOrEmpty(OwnerID10))
                        ListOwnerID.Add(OwnerID10);
                    #endregion

                    channellist = new List<Channel>();
                    for (int i = 0; i < ListOwnerID.Count; i++)
                    {
                        Criteria c = new Criteria();
                        c.Mode = CriteriaMode.And;
                        c.Add(CriteriaType.Equals, "State", 1);
                        c.Add(CriteriaType.Equals, "ID", ListOwnerID[i]);
                        List<Channel> templist = Assistant.List<Channel>(c, null, 0, ListOwnerID.Count, new string[]
                                                                         {
                                                                             "ID", "Title", "ChannelFullUrl", "Created", "SN"
                                                                         });
                        if (templist != null || templist.Count > 0)
                        {
                            channellist.Add(templist[0]);
                        }
                    }
                    for (int i = 0; i < channellist.Count; i++)
                    {
                        channellist[i].Articles = QueryArticlesByChannel(channellist[i], 0, PageSize);
                    }
                }
                return channellist;
            }
        }

        /// <summary>
        /// 根据栏目获取文章
        /// </summary>
        /// <param name="ch">栏目</param>
        /// <param name="includechildren">包含子栏目</param>
        /// <param name="from"></param>
        /// <param name="PageSize">页大小</param>
        List<Article> QueryArticlesByChannel(Channel ch, int from, int PageSize)
        {
            Criteria c = new Criteria(CriteriaType.None);
            if (HasChildrenChannel)
            {
                c.Add(CriteriaType.Like, "ChannelFullUrl", ch.FullUrl + "%");
                c.Add(CriteriaType.Equals, "State", 1);
            }
            else
            {
                c.Add(CriteriaType.Equals, "OwnerID", ch.ID);
                c.Add(CriteriaType.Equals, "State", 1);
            }
            if (!String.IsNullOrEmpty(Tags))
            {
                c.Add(CriteriaType.Like, "Tags", "%'" + Tags + "'%");
            }
            Order[] os = IsShow
                             ? new Order[] { new Order("IsShow", OrderMode.Desc), new Order("Updated", OrderMode.Desc) }
                             : new Order[] { new Order("Updated", OrderMode.Desc) };
            return Assistant.List<Article>(c, os, from, PageSize,
                                           new string[] { "ID", "Title", "Updated", "ChannelFullUrl", "Created", "SN" });
        }

        /// <summary>
        /// 自定义图标
        /// </summary>
        protected virtual string CustomIcon
        {
            get
            {
                return Icon;
            }
        }
        protected string BackgroundIcon()
        {
            if (!string.IsNullOrEmpty(CustomIcon))
            {
                return string.Format("style=\"background:url({0}) no-repeat;\"", CustomIcon);
            }
            return string.Empty;
        }
        /// <summary>
        /// 图片新闻
        /// </summary>
        protected List<Article> Pictures
        {
            get
            {
                if (pictureNews == null)
                {
                    Criteria c = new Criteria(CriteriaType.Equals, "ModelName", "System.Article");
                    c.Add(CriteriaType.Equals, "IsImage", 1);
                    c.Add(CriteriaType.Equals, "State", 1);
                    Order[] os = new Order[] { new Order("Updated", OrderMode.Desc) };
                    int count = HelperFactory.Instance.Assistant.Count<Article>(c);
                    pictureNews =
                        HelperFactory.Instance.Assistant.List<Article>(c, os, 0, SliderSize,
                                                                       new string[]
                                                                           {
                                                                               "ID", "Title", "ChannelFullUrl",
                                                                               "Created",
                                                                               "SN", "Thumbnail"
                                                                           }) ?? new List<Article>();
                }
                return pictureNews;
            }
        }

        protected string FlashSlideData
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                StringBuilder sb3 = new StringBuilder();
                for (int i = 0; i < Pictures.Count && i < SliderSize; i++)
                {
                    sb.AppendFormat("{0}|", Pictures[i].GetTagThumbnail(ThumbnailTag));
                    sb2.AppendFormat("{0}|", Pictures[i].Url);
                    sb3.AppendFormat("{0}|", Pictures[i].Title);
                }
                Utils.TrimEndStringBuilder(sb, "|");
                Utils.TrimEndStringBuilder(sb2, "|");
                Utils.TrimEndStringBuilder(sb3, "|");
                return "pics=" + sb + "&amp;links=" + sb2 + "&amp;texts=" + sb3;
            }
        }
    }
}